#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "settings.h"

int main(int argc, char *argv[]) {
	printf("Server start\n");

	int fd_listener = socket(AF_INET, SOCK_DGRAM, 0);
	if (fd_listener < 0) {
		perror("socket");
		return (1);
	}

	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(PORT);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);

	if (bind(fd_listener, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
		perror("bind");
		return (1);
	}

	while (1) {
		char buffer[BUFFER_SIZE];
		socklen_t len = sizeof(addr);
		int read = recvfrom(fd_listener, buffer, BUFFER_SIZE, 0,
				(struct sockaddr*) &addr, &len);
		if (read <= 0) {
			continue;
		}
		printf("%s", buffer);
		sendto(fd_listener, buffer, read, 0, (struct sockaddr*) &addr,
				sizeof(addr));
	}

	return (0);
}
